<?php 
class DomArListenersTestCase extends DomArTestUnitTestCase {

	public function testListeners() {
		DomArTestClass_Listeners::getTable()->update();
	
		$a = new DomArTestClass_Listeners();
		$a->save();
		$this->assertTrue( $a->beforeInsert );
		$this->assertTrue( $a->beforeInsertSingle );
		$this->assertTrue( $a->afterInsert );
		$this->assertTrue( $a->afterInsertSingle );
		$this->assertTrue( $a->beforeSave );
		$this->assertTrue( $a->afterSave );
		$this->assertTrue( $a->beforeSaveSingle );
		$this->assertTrue( $a->afterSaveSingle );

		$a->listenerVar = 'a';
		$this->assertTrue( $a->beforeValidate );
		$this->assertTrue( $a->beforeSet );
		$this->assertTrue( $a->afterSet );

		$a->setChanged(false);
		
		$a->beforeSet = false;
		$a->afterSet = false;
	
		$a->listenerVar = NULL;
		$this->assertTrue( $a->isChanged('listenerVar') );
		$this->assertTrue( $a->beforeValidate );
		$this->assertTrue( $a->beforeSet );
		$this->assertTrue( $a->afterSet );
		
		$a->beforeSave = false;
		$a->afterSave = false;
		$a->beforeSaveSingle = false;
		$a->afterSaveSingle = false;
		
		$a->save();
		$this->assertTrue( $a->beforeUpdate );
		$this->assertTrue( $a->beforeUpdateSingle );
		$this->assertTrue( $a->afterUpdate );
		$this->assertTrue( $a->afterUpdateSingle );
		$this->assertTrue( $a->beforeSave );
		$this->assertTrue( $a->afterSave );
		$this->assertTrue( $a->beforeSaveSingle );
		$this->assertTrue( $a->afterSaveSingle );
		
		$a->delete();
		$this->assertTrue( $a->beforeDelete );
		$this->assertTrue( $a->beforeDeleteSingle );
		$this->assertTrue( $a->afterDelete );
		$this->assertTrue( $a->afterDeleteSingle );
				
		$this->assertTableEmpty( $a->getTableName() );

	}
	
	public function testExtListeners() {
		DomArTestClass_ListenersExt::getTable()->update();
	
		$a = new DomArTestClass_ListenersExt();
		$a->save();
		$this->assertTrue( $a->beforeInsert );
		$this->assertTrue( $a->beforeInsertSingle );
		$this->assertTrue( $a->afterInsert );
		$this->assertTrue( $a->afterInsertSingle );
		$this->assertTrue( $a->beforeSave );
		$this->assertTrue( $a->afterSave );
		$this->assertTrue( $a->beforeSaveSingle );
		$this->assertTrue( $a->afterSaveSingle );

		$a->listenerVar = 'a';
		$this->assertTrue( $a->beforeValidate );
		$this->assertTrue( $a->beforeSet );
		$this->assertTrue( $a->afterSet );

		$a->beforeSave = false;
		$a->afterSave = false;
		$a->beforeSaveSingle = false;
		$a->afterSaveSingle = false;

		$a->save();
		$this->assertTrue( $a->beforeUpdate );
		$this->assertTrue( $a->beforeUpdateSingle );
		$this->assertTrue( $a->afterUpdate );
		$this->assertTrue( $a->afterUpdateSingle );
		$this->assertTrue( $a->beforeSave );
		$this->assertTrue( $a->afterSave );
		$this->assertTrue( $a->beforeSaveSingle );
		$this->assertTrue( $a->afterSaveSingle );
		
		$a->delete();
		$this->assertTrue( $a->beforeDelete );
		$this->assertTrue( $a->beforeDeleteSingle );
		$this->assertTrue( $a->afterDelete );
		$this->assertTrue( $a->afterDeleteSingle );
				
		$this->assertTableEmpty( $a->getTableName() );

	}
	
	
	public function testOverwritingListeners() {
		// These tests are yet not working correctly.
		return;
		
		DomArTestClass_ListenersOvr::getTable()->update();
	
		$a = new DomArTestClass_ListenersOvr();
		$a->save();
		$this->assertTrue( $a->beforeInsert );
		$this->assertFalse( $a->beforeInsertSingle );
		$this->assertTrue( $a->afterInsert );
		$this->assertFalse( $a->afterInsertSingle );
		$this->assertTrue( $a->beforeSave );
		$this->assertTrue( $a->afterSave );
		$this->assertFalse( $a->beforeSaveSingle );
		$this->assertFalse( $a->afterSaveSingle );
		
		$this->assertTrue( $a->beforeInsertOvr  );
		$this->assertTrue( $a->beforeInsertSingleOvr  );
		$this->assertTrue( $a->afterInsertOvr  );
		$this->assertTrue( $a->afterInsertSingleOvr  );
		$this->assertTrue( $a->beforeSaveOvr  );
		$this->assertTrue( $a->afterSaveOvr  );
		$this->assertTrue( $a->beforeSaveSingleOvr  );
		$this->assertTrue( $a->afterSaveSingleOvr  );

		$a->listenerVar = 'a';
		$this->assertFalse( $a->beforeValidate );
		$this->assertFalse( $a->beforeSet );
		$this->assertFalse( $a->afterSet );
		$this->assertTrue( $a->beforeValidateOvr );
		$this->assertTrue( $a->beforeSetOvr  );
		$this->assertTrue( $a->afterSetOvr  );
		
		$a->beforeSave = false;
		$a->afterSave = false;
		$a->beforeSaveSingle = false;
		$a->afterSaveSingle = false;

		$a->save();
		$this->assertTrue( $a->beforeUpdate );
		$this->assertFalse( $a->beforeUpdateSingle );
		$this->assertTrue( $a->afterUpdate );
		$this->assertFalse( $a->afterUpdateSingle );
		$this->assertTrue( $a->beforeSave );
		$this->assertTrue( $a->afterSave );
		$this->assertFalse( $a->beforeSaveSingle );
		$this->assertFalse( $a->afterSaveSingle );
		
		$this->assertTrue( $a->beforeUpdateOvr  );
		$this->assertTrue( $a->beforeUpdateSingleOvr  );
		$this->assertTrue( $a->afterUpdateOvr  );
		$this->assertTrue( $a->afterUpdateSingleOvr  );
		$this->assertTrue( $a->beforeSaveOvr  );
		$this->assertTrue( $a->afterSaveOvr  );
		$this->assertTrue( $a->beforeSaveSingleOvr  );
		$this->assertTrue( $a->afterSaveSingleOvr  );
		
		$a->delete();
		$this->assertTrue( $a->beforeDelete );
		$this->assertFalse( $a->beforeDeleteSingle );
		$this->assertTrue( $a->afterDelete );
		$this->assertFalse( $a->afterDeleteSingle );
		
		$this->assertTrue( $a->beforeDeleteOvr  );
		$this->assertTrue( $a->beforeDeleteSingleOvr  );
		$this->assertTrue( $a->afterDeleteOvr  );
		$this->assertTrue( $a->afterDeleteSingleOvr  );
				
		$this->assertTableEmpty( $a->getTableName() );

	}

	public function testRelationCallingListeners() {
		
		$a = new DomArTestClass_RelListeners();
		$b = new DomArTestClass_RelListeners();
		$c = new DomArTestClass_RelListeners();
		
		$a->child = $b;
		
		$this->assertTrue( $a->bef2 );
		$this->assertFalse( $a->bef1 );
		$this->assertTrue( $b->bef1 );
		$this->assertFalse( $b->bef2 );
		
		$a->children[] = $c;
		$this->assertFalse( $a->bef3 );
		$this->assertTrue( $c->bef3 );
	
	}
	
	
}
?>